#!/usr/bin/env python3
# -*- coding:utf-8 -*-
## author : mike
## date   : 2014.12.5
"""
"""

import os 
import sys
from os.path import realpath, dirname, basename, isdir
sys.path.append(dirname(dirname(realpath(__file__))))
from xbc3.fileio import FileIO

class Proportion(object):
    """docstring for Proportion"""
    def __init__(self, *inputfiles):
        super(Proportion, self).__init__()
        self.ss_files_name = []
        self.cpv_files_name = []
        for fn in inputfiles:
            if isdir(fn):
                continue
            if 'sys_ss' in basename(fn):
                self.ss_files_name.append(fn)
            if 'sys_cpv' in  basename(fn):
                self.cpv_files_name.append(fn)
        if not self.cpv_files_name :
            print('Error:can not find files!!!\n', file=sys.stderr)
            sys.exit()
        self.ss_cookie_dict={}
        self.ss_times_dict={}
        self.cpv_cookie_set=set()
        self.output_dict={}


    def get_ss_cookie_dict(self,filename):
        print(filename,'get_ss_cookie_dict\n')
        file = FileIO(filename)
        for line in file:  
            try:
                cookie = line[44:76]
                times =int(line[83:line.find('|user')])
            except Exception as e:
                pass
            if cookie in self.ss_cookie_dict:
                if self.ss_cookie_dict[cookie]<times:
                    self.ss_cookie_dict[cookie]=times
                else:
                    pass
            else:
                self.ss_cookie_dict[cookie]=times

    def get_ss_times_dict(self,ss_cookie_dict):

        for k,v in ss_cookie_dict.items():
            if v in self.ss_times_dict:
                self.ss_times_dict[v].append(k)
            else: 
                self.ss_times_dict[v]=[k]
            


    def get_cpv_cookie_set(self,filename):
        print(filename,'get_cpv_cookie_set\n')
        file = FileIO(filename)
        for line in file:  
            try:
                cookie = line[line.find('|user_value_cookie:')+19:line.find('|user-agent:')]
                self.cpv_cookie_set.add(cookie)
            except Exception as e:
                pass

    def get_proportion(self,times_list):
        l=len(set(times_list) & self.cpv_cookie_set)
        return [l,len(times_list),100*l/len(times_list)]

    def output(self):
        fpath=os.path.split(self.ss_files_name[0])[0]
        result_path=os.path.join(fpath,'result.csv')
        
        with FileIO(result_path,'w') as f:
            f.write('times,,,Proportion\n')
            for k in self.output_dict:
                v = self.output_dict[k]
                f.write('%s, %d, %d, %f%%\n' % (k, v[0],v[1],v[2]))

    def run(self):
        for fn in self.cpv_files_name:
            self.get_cpv_cookie_set(fn)
        for fn in self.ss_files_name:
            self.get_ss_cookie_dict(fn)
        self.get_ss_times_dict(self.ss_cookie_dict)
        for i in self.ss_times_dict:
            self.output_dict[i]=self.get_proportion(self.ss_times_dict[i])
        self.output()


if __name__ == '__main__':
    '''driver'''
    inputfiles = []
    for line in sys.stdin:
        line = line.rstrip('\r\n')
        inputfiles.append(line)
    p=Proportion(*inputfiles)
    p.run()